home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / overview / example / appletbutton.000 next >
Encoding:
Text File  |  1996-02-26  |  4.5 KB  |  169 lines

  1. <!--NewPage-->
  2. <html>
  3. <head>
  4. <title>AppletButton.java</title>
  5. </head>
  6. <body>
  7. <p>
  8. <hr size=4>
  9.  
  10. <h2>
  11.     AppletButton.java
  12. </h2>
  13. <p>
  14. <blockquote>
  15.  
  16. <pre>
  17.  
  18. /*
  19.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software
  22.  * and its documentation for NON-COMMERCIAL purposes and without
  23.  * fee is hereby granted provided that this copyright notice
  24.  * appears in all copies. Please refer to the file "copyright.html"
  25.  * for further important copyright and licensing information.
  26.  *
  27.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  28.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  29.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  30.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  31.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  32.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  33.  */
  34. import java.awt.*;
  35. import java.util.*;
  36. import java.applet.Applet;
  37.  
  38. public class AppletButton extends Applet implements Runnable {
  39.     int frameNumber = 1;
  40.     String windowClass = null;
  41.     String buttonText = null;
  42.     String windowText = null;
  43.     Button button = null;
  44.     Thread windowThread = null;
  45.     Label label = null;
  46.     boolean pleaseCreate = false;
  47.  
  48.     public void init() {
  49.     windowClass = getParameter("WINDOWTYPE");
  50.     if (windowClass == null) {
  51.         windowClass = "TestWindow";
  52.     }
  53.  
  54.     buttonText = getParameter("BUTTONTEXT");
  55.     if (buttonText == null) {
  56.         buttonText = "Click here to bring up a " + windowClass;
  57.     }
  58.  
  59.     windowText = getParameter("WINDOWTEXT");
  60.     if (windowText == null) {
  61.         windowText = windowClass;
  62.     }
  63.  
  64.     setLayout(new GridLayout(2,0));
  65.     add(button = new Button(buttonText));
  66.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  67.  
  68.     add(label = new Label("", Label.CENTER));
  69.     }
  70.  
  71.     public void start() {
  72.     if (windowThread == null) {
  73.         windowThread = new Thread(this, windowClass + " Bringup Thread");
  74.         windowThread.start();
  75.     }
  76.     }
  77.  
  78.     public synchronized void run() {
  79.     Class windowClassObject = null;
  80.     Class tmp = null;
  81.     String name = null;
  82.     
  83.     // Make sure the window class exists.
  84.     // This has the added benefit of pre-loading the class,
  85.     // which makes it much quicker for the first window to come up.
  86.     try {
  87.         windowClassObject = Class.forName(windowClass);
  88.     } catch (Exception e) {
  89.         // The specified class isn't anywhere that we can find.
  90.         label.setText("Can't create window: Couldn't find class "
  91.                   + windowClass);
  92.         button.disable();
  93.     }
  94.  
  95.     // Make sure the class is a Frame.
  96.     for (tmp = windowClassObject, name = tmp.getName();
  97.          !( name.equals("java.lang.Object") ||
  98.             name.equals("java.awt.Frame") ); ) {
  99.         tmp = tmp.getSuperclass();
  100.         name = tmp.getName();
  101.     }
  102.     if ((name == null) || name.equals("java.lang.Object")) {
  103.         //We can't run; ERROR; print status, never bring up window
  104.         label.setText("Can't create window: "
  105.                   + windowClass +
  106.               " isn't a Frame subclass.");
  107.         button.disable();
  108.     } else if (name.equals("java.awt.Frame")) { 
  109.  
  110.         //Everything's OK. Wait until we're asked to create a window.
  111.         while (windowThread != null) {
  112.             while (pleaseCreate == false) {
  113.             try {
  114.                 wait();
  115.             } catch (InterruptedException e) {
  116.             }
  117.             }
  118.  
  119.             //We've been asked to bring up a window.
  120.             pleaseCreate = false;
  121.             Frame window = null;
  122.             try {
  123.                 window = (Frame)windowClassObject.newInstance();
  124.             } catch (Exception e) {
  125.             label.setText("Couldn't create instance of class "
  126.                       + windowClass);
  127.             }
  128.         if (frameNumber == 1) {
  129.                 window.setTitle(windowText);
  130.         } else {
  131.                 window.setTitle(windowText + ": " + frameNumber);
  132.         }
  133.             frameNumber++;
  134.             window.pack();
  135.             window.show();
  136.             label.setText("");
  137.         }
  138.     }
  139.     }
  140.  
  141.     public void stop() {
  142.         windowThread.stop();
  143.         windowThread = null;
  144.     }
  145.         
  146.     public synchronized boolean action(Event event, Object what) {
  147.     if (event.target instanceof Button) {
  148.         //signal the window thread to build a window
  149.         label.setText("Please wait while the window comes up...");
  150.         pleaseCreate = true;
  151.         notify();
  152.     } 
  153.     return false;
  154.     }
  155. }
  156.  
  157. class TestWindow extends Frame {
  158.     public TestWindow() {
  159.     resize(300, 300);
  160.     }
  161. }
  162. </pre>
  163. </blockquote>
  164. <p>
  165. <hr size=4>
  166. <p>
  167. </body>
  168. </html>
  169.